"""
@Author: marco
@Date: 2021-6-17 09:52:51
@description: 返回结果
@LastEditors: marco
@LastEditTime: 2021-6-17 09:52:51
"""

from flask import request, jsonify

from app.enums.code import Code
from app.utils.logger import Logger
from app.env import DEBUG_LOG
from app.utils.common import CommonUtil


class JR:
    """
        * 返回Json数据
        * @param  dict body
        * @return json
        """

    def json(self, body=None):
        if body is None:
            body = {}
        if DEBUG_LOG:
            debug_id = CommonUtil.unique_id()
            data = {
                'LOG_ID': debug_id,
                'IP_ADDRESS': request.remote_addr,
                'REQUEST_URL': request.url,
                'REQUEST_METHOD': request.method,
                'PARAMETERS': request.args,
                'RESPONSES': body
            }

            Logger.all_log().logger.debug(data)
            body['debug_id'] = debug_id
        return jsonify(body)

    """
    * 返回错误信息
    * @param  msg string
    * @return json
    """

    def error(self, msg='操作失败'):
        return self.json({'code': Code.BAD_REQUEST, 'msg': msg, 'data': None})

    '''
    * 返回成功信息
    * @param  msg string
    * @return json
    '''

    def success(self, data='', msg='操作成功'):
        return self.json({'code': Code.SUCCESS, 'msg': msg, 'data': data})
